# -*- coding: utf-8 -*-
"""
Created on Mon Dec 20 16:20:16 2021

@author: wulong
"""
import numpy as np
from casadi import *
from Fast_model import* 

# In[1] Fast MPC
def formulate_opt_f(initial_state, state_guess, state_bnds_lo, state_bnds_up, 
                    fast_state_slack_lo, fast_state_slack_up, 
                    slow_state_bnds, 
                    medium_state_bnds, 
                    slow_input_bnds, 
                    medium_input_bnds, 
                    input_guess, input_bnds_lo, input_bnds_up, 
                    input_bin_bnds,
                    distb_bnds,
                    output_guess, 
                    output_setpnts, fast_state_setpnts, fast_input_setpnts, 
                    alpha, alpha_xf, alpha_uf, alpha_slack, pred_horzn):
    w = []
    w0 = []
    lbw = []
    ubw = []
    J = 0
    g = []
    lbg = []
    ubg = []
    
    # "Lift" initial states conditions
    xname = 'X' + str(0)
    Xk = MX.sym(xname, Nx_f)
    w += [Xk]
    lbw += [initial_state]
    ubw += [initial_state]
    w0 += [initial_state]
    
    for k in range(1, pred_horzn+1):
        # Define binary for inputs
        Zu_f = vertcat(input_bin_bnds[0], input_bin_bnds[2])
        
        # Create input variables
        uname = 'U' + str(k-1)
        Uk = MX.sym(uname, Nuc_f)
        w += [Uk]
        lbw +=[Zu_f*input_bnds_lo]
        ubw +=[Zu_f*input_bnds_up]
        w0 += [input_guess]
        
        # Create disturbance
        Dk = distb_bnds[k-1,:]
        
        # Simulate the model
        Ik = I_ode_f(x0 = Xk, p = vertcat(slow_state_bnds, medium_state_bnds, 
                                          slow_input_bnds, medium_input_bnds, Uk, 
                                          input_bin_bnds, Dk))
        X_int = Ik['xf']
        
        # Create new states variables
        xname = 'X' + str(k)
        Xk = MX.sym(xname, Nx_f)
        w += [Xk]
        lbw += [state_bnds_lo]
        ubw += [state_bnds_up]
        w0 += [state_guess]
        
        # Add dynamic constraints
        g += [X_int - Xk]
        lbg += [[0]*Nx_f]
        ubg += [[0]*Nx_f]
        
        # Create outputs variables
        yname = 'y' + str(k)
        Yk = MX.sym(yname, Ny_f)
        w += [Yk]
        lbw += [[-np.inf]*Ny_f] 
        ubw += [[np.inf]*Ny_f]
        w0 += [output_guess]
        
        # Add output constraints
        g += [Yk - out_ies_f(Xk, slow_state_bnds, medium_state_bnds, 
                             slow_input_bnds, medium_input_bnds, Uk, 
                             input_bin_bnds, Dk)]
        lbg += [[0]*Ny_f]
        ubg += [[0]*Ny_f]
        
        # Add slack variables for fast states
        ename = 'e' + str(k)
        ek = MX.sym(ename, 4)
        w += [ek]
        lbw += [[0]*4] 
        ubw += [[np.inf]*4]
        w0 += [[0]*4]
        
        Xfk_slack = vertcat(Xk[7], Xk[8])
        ek_lo = vertcat(ek[0], ek[2])
        ek_up = vertcat(ek[1], ek[3])
        g += [Xfk_slack + ek_lo - ek_up]
        lbg += [fast_state_slack_lo]
        ubg += [fast_state_slack_up]
        
        # Create tracking devation term for medium and fast states and inputs
        delta_Xf = Xk - fast_state_setpnts
        delta_Uf = Uk - fast_input_setpnts
        
        # The cost funtion
        J += alpha[0] * (Yk[0] - output_setpnts[k-1])**2
        J += alpha[1] * Uk[0]
        J += mtimes(mtimes(delta_Xf.T, alpha_xf), delta_Xf)
        J += mtimes(mtimes(delta_Uf.T, alpha_uf), delta_Uf)
        J += mtimes(mtimes(ek.T, alpha_slack), ek)
        
        pass
        
    # Concatenate decision variables and constraint terms
    w = vertcat(*w)
    lbw = vertcat(*lbw)
    ubw = vertcat(*ubw)
    w0 = vertcat(*w0)
    g = vertcat(*g)
    lbg = vertcat(*lbg)
    ubg = vertcat(*ubg)
    
    return w, lbw, ubw, w0, g, lbg, ubg, J

# In[2] Creat Fast MPC solver
def solve_opt_f(w, lbw, ubw, w0, g, lbg, ubg, J):
    print('Creat an F-MPC solver')
    nlp_prob = {'f': J, 'x': w, 'g': g}
    nlp_solver = nlpsol('nlp_solver', 'ipopt', nlp_prob)
    # Solve NLP
    print('Solve F-MPC')
    # nlp_solver.stats()
    sol = nlp_solver(x0=w0, lbx=lbw, ubx=ubw, lbg=lbg, ubg=ubg)
    print(nlp_solver.stats())
    optimalValues = sol['x'].full().ravel()
    
    return optimalValues